package com.common.msg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.core.controller.BaseController;
import com.common.core.domain.entity.SysUser;
import com.common.core.page.PageDomain;
import com.common.core.page.TableDataInfo;
import com.common.msg.entity.*;
import com.common.msg.service.SysMessageService;
import com.common.utils.PageUtils;
import com.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "系统消息")
@RestController
@RequestMapping("/xtxx")
public class SysMessageController extends BaseController {
    @Autowired
    SysMessageService sysMessageService;

    @PostMapping("/sysMessageList")
    @ApiOperation(value = "查询系统消息")
    public Result sysMessageList(@RequestBody SysMessageVo sysMessageVo){
        CutPageBean<SysMessageBo> cutPageBean =sysMessageService.sysMessageList(sysMessageVo);
        return Result.succ(cutPageBean);
    }

    //isRead(0是all,1是已读，2是未读)
    @PostMapping("/insertSysMessage")
    @ApiOperation(value = "新增系统消息")
    public Result insertSysMessage(@RequestBody SysMessageBo sysMessageBo){
        int row=sysMessageService.insertSysMessage(sysMessageBo);
        if (row > 0) {
            return Result.succ("新增成功");
        } else {
            return Result.fail("新增失败");
        }
    }

    //isRead(0是all,1是已读，2是未读)
    @PostMapping("/updateSysMessage")
    @ApiOperation(value = "更新系统消息")
    public Result updateSysMessage(@RequestBody SysMessageBo sysMessageBo){
        int row=sysMessageService.updateSysMessage(sysMessageBo);
        if (row > 0) {
            return Result.succ("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    @PostMapping("/updateAllRead")
    @ApiOperation(value = "全部置为已读")
    public Result updateAllRead(){
        int row=sysMessageService.updateAllRead();
        if (row > 0) {
            return Result.succ("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    @GetMapping("/unreadSysMessage")
    @ApiOperation(value = "未读消息")
    public TableDataInfo unreadSysMessage(PageDomain pageDomain,SysMessageVo sysMessageVo){
        PageUtils.startPage();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        sysMessageVo.setId(String.valueOf(userId));
        List<SysMessageBo> sysMessageBos = sysMessageService.unreadSysMessage(pageDomain, sysMessageVo);
        return getDataTable(sysMessageBos);
    }

    @GetMapping("/readSysMessage")
    @ApiOperation(value = "已读消息")
    public TableDataInfo readSysMessage(PageDomain pageDomain,SysMessageVo sysMessageVo){
        PageUtils.startPage();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        sysMessageVo.setId(String.valueOf(userId));
        List<SysMessageBo> sysMessageBos = sysMessageService.readSysMessage(pageDomain, sysMessageVo);
        return getDataTable(sysMessageBos);
    }

    @PostMapping("/messageTemplate")
    @ApiOperation(value = "消息模板")
    public Result MessageList(@RequestBody SysMsgRuleVo sysMsgRuleVo){
        String list =sysMessageService.replacePlaceholders(sysMsgRuleVo);
        return Result.succ(list);
    }
}
